package baseclass.m_graph;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

/**
 * @date 2020/3/6 20:14
 */
public class Code01_BFS {
    //广度优先遍历：
    public static void bfs(Node node) {
        if(node == null) return;
        Queue<Node> queue = new LinkedList<>();
        Set<Node> visited = new HashSet<>();
        queue.offer(node);
        visited.add(node);
        while (!queue.isEmpty()){
            node = queue.poll();
            System.out.print(node.val+"->");
            for (Node next : node.nexts) {
                if(!visited.contains(next)){
                    queue.offer(next);
                    visited.add(next);
                }
            }
        }
    }
    public static void main(String[] args) {
        Integer[][]matrix = {{-1,1,2},{-1,1,3},{-1,2,3},{-1,1,4},{-1,2,7},{-1,7,3},{-1,
                3,5},{-1,4,6}};
        Graph graph = Graph.createGraph(matrix);
        bfs(graph.nodes.get(1));

    }
}
